---
title: "Harris-Freeman, Crossing a Virtual Divide, Code"
author: "Benjamin Norwood Harris"
date: "2/2/2023"
output: pdf_document
---



```{r, include = FALSE}
# call-up needed functions 
library(lmtest) #used later for the function "coeftest"
library(ggplot2) #used later for graph

```

```{r}
# read in the data

Pre <- read.csv("Pre-Questionnaire, Crisis Sim.csv")
Post <- read.csv("Post-Questionnaire, Crisis Sim.csv")
```



```{r}
## make a subset of just free response for easier viewing 

FreeResponse <- subset(Post, select = c(Q6, Q41, Q42, Q43))


```



```{r}

## replace qualitative names with numeric levels 
## there is certainly a better way to do this
## Strong agree = 5, strong disagree = 1 

##start with Pre 

Pre <- data.frame(lapply(Pre, function(x) {
                  gsub("Strongly agree", 5, x)
              })) ##strongly agree

Pre <- data.frame(lapply(Pre, function(x) {
                  gsub("Strongly disagree", 1, x)
              })) ##strongly disagree

Pre <- data.frame(lapply(Pre, function(x) {
                  gsub("Neither agree nor disagree", 3, x)
              })) ##Neither agree nor disagree

Pre <- data.frame(lapply(Pre, function(x) {
                  gsub("Agree", 4, x)
              })) ##Agree

Pre <- data.frame(lapply(Pre, function(x) {
                  gsub("Disagree", 2, x)
              })) ##Disagree

Pre <- data.frame(lapply(Pre, function(x) {
                  gsub("Disagree", 2, x)
              })) ##Disagree

##also do the military/civilian question; mil=1, civ=0

Pre <- data.frame(lapply(Pre, function(x) {
                  gsub("Serving or served in the military", 1, x)
              })) ##Mil

Pre <- data.frame(lapply(Pre, function(x) {
                  gsub("Never served in the military", 0, x)
              })) ##Civ

##now same thing for Post 

Post <- data.frame(lapply(Post, function(x) {
                  gsub("Strongly agree", 5, x)
              })) ##strongly agree

Post <- data.frame(lapply(Post, function(x) {
                  gsub("Strongly disagree", 1, x)
              })) ##strongly disagree

Post <- data.frame(lapply(Post, function(x) {
                  gsub("Neither agree nor disagree", 3, x)
              })) ##Neither agree nor disagree

Post <- data.frame(lapply(Post, function(x) {
                  gsub("Agree", 4, x)
              })) ##Agree

Post <- data.frame(lapply(Post, function(x) {
                  gsub("Disagree", 2, x)
              })) ##Disagree


##also do the military/civilian question; mil=1, civ=0


Post <- data.frame(lapply(Post, function(x) {
                  gsub("Serving or served in the military", 1, x)
              })) ##Mil

Post <- data.frame(lapply(Post, function(x) {
                  gsub("Never served in the military", 0, x)
              })) ##Civ

```



```{r, tidy=TRUE}
## averages and t-tests 
## each section below, separated by single "#" marks, returns five values 
## 1) Pre mean; 2) post mean; 3) post mean - pre mean; 4) that diff as a % 
## 5) Welch Two Sample t-test 
## the test is not paired, which reduces power, but is necessary because we  
## don't have same number of responses, so it's not a 1 to 1 


######################Relevant to First Learning Objective 

#Q8 - I have a good understanding of the political and military factors that would affect
#how a Taiwan crisis scenario would unfold 

mean(as.numeric(Pre$Q8))
mean(as.numeric(Post$Q8))

mean(as.numeric(Post$Q8)) - mean(as.numeric(Pre$Q8))
(mean(as.numeric(Post$Q8)) - mean(as.numeric(Pre$Q8)))*100/5


PreQ8Numeric <- as.numeric(Pre$Q8)
PostQ8Numeric <- as.numeric(Post$Q8)

t.test(x = PreQ8Numeric, y = PostQ8Numeric, paired = FALSE, alternative = "two.sided")


```

```{r, tidy=TRUE}
######################Relevant to Third  Learning Objective 

#Q16 - Wargames are a useful tool to study important foreign policy decisions.


mean(as.numeric(Pre$Q16))
mean(as.numeric(Post$Q16))

mean(as.numeric(Post$Q16)) - mean(as.numeric(Pre$Q16))
(mean(as.numeric(Post$Q16)) - mean(as.numeric(Pre$Q16)))*100/5


PreQ16Numeric <- as.numeric(Pre$Q16) 
PostQ16Numeric <- as.numeric(Post$Q16)

t.test(x = PreQ16Numeric, y = PostQ16Numeric, paired = FALSE, alternative = "two.sided")


#Q17 - Wargames can accurately capture the information uncertainty and decision-making
#dynamics of real life crisis scenarios.


mean(as.numeric(Pre$Q17))
mean(as.numeric(Post$Q17))

mean(as.numeric(Post$Q17)) - mean(as.numeric(Pre$Q17))
(mean(as.numeric(Post$Q17)) - mean(as.numeric(Pre$Q17)))*100/5


PreQ17Numeric <- as.numeric(Pre$Q17) 
PostQ17Numeric <- as.numeric(Post$Q17)

t.test(x = PreQ17Numeric, y = PostQ17Numeric, paired = FALSE, alternative = "two.sided")

#Q18 - In-person wargames have an energy and dynamic that can’t be matched by a
#remote alternative


mean(as.numeric(Pre$Q18))
mean(as.numeric(Post$Q18), na.rm = TRUE) #had one NA value here

mean(as.numeric(Post$Q18), na.rm = TRUE) - mean(as.numeric(Pre$Q18))
(mean(as.numeric(Post$Q18), na.rm = TRUE) - mean(as.numeric(Pre$Q18)))*100/5


PreQ18Numeric <- as.numeric(Pre$Q18) 
PostQ18Numeric <- as.numeric(na.omit(Post$Q18))



t.test(x = PreQ18Numeric, y = PostQ18Numeric, paired = FALSE, alternative = "two.sided")


```


```{r}

# This section displays the above graphically 
# The "military" variable is whether the respondent has military affiliation 
# Is is the only collected demographic control variable 


##subset and name data 
Pre.sub <- subset(Pre, select = c(Q16, Q17, Q18, Q6))
colnames(Pre.sub) <- c("Useful_Tool", "Real_Life", "In_Person", "Military")
Pre.sub$post <- 0 
Post.sub <- subset(Post, select = c(Q16, Q17, Q18, Q6))
colnames(Post.sub) <- c("Useful_Tool", "Real_Life", "In_Person", "Military")
Post.sub$post <- 1 

#combine
Combined_DF <- rbind(Pre.sub, Post.sub)

##regressions 
UT_1 <- lm(Useful_Tool ~ post , data = Combined_DF)
UT_5 <- lm(Useful_Tool ~ post + Military, data = Combined_DF)

RL_1 <- lm(Real_Life ~ post , data = Combined_DF)
RL_5 <- lm(Real_Life ~ post + Military, data = Combined_DF)

IP_1 <- lm(In_Person ~ post , data = Combined_DF)
IP_5 <- lm(In_Person ~ post + Military, data = Combined_DF)

##combining regressions into a dataframe for graphing 
Results_DF_1 <- as.data.frame(matrix(data = c("Useful Tool", coeftest(UT_1)[2, 1:2],"no", 
                                   "Useful Tool", coeftest(UT_5)[2, 1:2], "yes",
                                   "Real Life", coeftest(RL_1)[2, 1:2], "no",
                                   "Real Life", coeftest(RL_5)[2, 1:2], "yes",
                                   "In Person", coeftest(IP_1)[2, 1:2], "no",
                                   "In Person", coeftest(IP_5)[2, 1:2], "yes"), 
                                   ncol = 4, byrow = TRUE))

#naming 
colnames(Results_DF_1) <- c("dv", "estimate", "se", "controls")

#making into correct operators 
Results_DF_1$dv <- factor(Results_DF_1$dv, levels = c("Useful Tool", "Real Life", "In Person"))
Results_DF_1$estimate <- as.numeric(Results_DF_1$estimate)
Results_DF_1$se <- as.numeric(Results_DF_1$se)

#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))
Results_DF_1$ci <- Results_DF_1$se*q


## graph time 
pd <- position_dodge(0.5)

ggplot(Results_DF_1, aes(x = dv, y = estimate, color = controls))  + 
  geom_point(aes(color=controls, shape=controls), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Question") + ylab("Average Treatment Effect")  + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1)) + coord_flip()

#unused title: ggtitle("Wargaming Questions, Average Treatment Effect of the Wargame")
```

```{r, tidy=TRUE}
## Now for all the solely post-crisis survey questions 

##############################################first learning objective 

# Q35 - I feel like I learned a lot from the crisis simulation.

mean(as.numeric(Post$Q35))

#let's do a summary to see the distribution 
table(Post$Q35)

```


```{r, tidy=TRUE}

##############################################second learning objective 

# Q36 - I feel that communication between my team and the other subnational team in my country was good.

mean(as.numeric(Post$Q36), na.rm = TRUE) #one non-response to remove
```


```{r}
# I will make a function for calculating standard errors 

std <- function(a) sd(a) / sqrt(length(a))



```


```{r, tidy=TRUE}
############################################## third learning objective 

#Q31 - I was bored during a lot of the crisis simulation.
mean(as.numeric(Post$Q31))
std(as.numeric(Post$Q31))
```


```{r, tidy=TRUE}
#Q34 - I was able to predict what would happen during the crisis simulation.
mean(as.numeric(Post$Q34))
std(as.numeric(Post$Q34))
```


```{r, tidy=TRUE}
#Q37 - I generally had the information I needed to make good decisions.
mean(as.numeric(Post$Q37))
std(as.numeric(Post$Q37))
```


```{r, tidy=TRUE}
#Q38 - I wanted to communicate with another team but was restrained from doing so.
mean(as.numeric(Post$Q38), na.rm = TRUE) #one non-response to remove
sd(as.numeric(Post$Q38), na.rm = TRUE)/sqrt(28)

```





